package com.tangr.adapterapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.tangr.normaladapter.ViewHolder;
import com.tangr.normaladapter.recyclerview.OnItemClickListener;
import com.tangr.normaladapter.recyclerview.RecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

public class CustomPullActivity extends AppCompatActivity {
    private JDPullToRefreshRecyclerview recyclerview;
    private List<String> mDatas = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_pull);

        initDatas();
        recyclerview = (JDPullToRefreshRecyclerview) findViewById(R.id.custom_pull_recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        final RecyclerAdapter<String> adapter = new RecyclerAdapter<String>(this,mDatas,R.layout.list_item) {
            @Override
            public void setView(ViewHolder holder, String item) {
                holder.setText(R.id.item_tv,item);
                holder.setImage(R.id.item_iv, R.mipmap.ic_launcher);
            }
        };
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, View view, Object o, int position) {
                Toast.makeText(CustomPullActivity.this,"click:"+position,Toast.LENGTH_SHORT).show();
            }
            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, Object o, int position) {
                Toast.makeText(CustomPullActivity.this,"long click:"+position,Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        recyclerview.setOnRefreshListener(new JDPullToRefreshRecyclerview.PullToRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        mDatas.clear();
                        refreshDatas();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter.notifyDataSetChanged();
                                recyclerview.onRefreshComplete();
                            }
                        });
                    }
                }).start();
            }
        });
        recyclerview.setAdapter(adapter);
    }

    private void initDatas()
    {
        for (int i = 'A'; i < 'T'; i++)
        {
            mDatas.add((char) i + "");
        }
    }

    private void refreshDatas()
    {
        for (int i = 'D'; i < 'N'; i++)
        {
            mDatas.add((char) i + "");
        }
    }
}
